<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>WebVNC 系统初始化</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .container {
            background: white;
            border-radius: 15px;
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
            padding: 40px;
            max-width: 500px;
            width: 90%;
            text-align: center;
        }

        .logo {
            font-size: 2.5em;
            margin-bottom: 10px;
            color: #667eea;
        }

        h1 {
            color: #333;
            margin-bottom: 10px;
            font-size: 2em;
        }

        .subtitle {
            color: #666;
            margin-bottom: 30px;
            font-size: 1.1em;
        }

        .form-group {
            margin-bottom: 20px;
            text-align: left;
        }

        label {
            display: block;
            margin-bottom: 8px;
            color: #555;
            font-weight: 500;
        }

        input[type="text"], input[type="password"] {
            width: 100%;
            padding: 12px 15px;
            border: 2px solid #e1e5e9;
            border-radius: 8px;
            font-size: 16px;
            transition: border-color 0.3s;
        }

        input[type="text"]:focus, input[type="password"]:focus {
            outline: none;
            border-color: #667eea;
        }

        .btn {
            width: 100%;
            padding: 12px 20px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            border-radius: 8px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: transform 0.2s, box-shadow 0.2s;
        }

        .btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 10px 20px rgba(102, 126, 234, 0.3);
        }

        .btn:disabled {
            opacity: 0.6;
            cursor: not-allowed;
            transform: none;
        }

        .alert {
            padding: 12px;
            border-radius: 8px;
            margin-bottom: 20px;
            display: none;
        }

        .alert-success {
            background-color: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }

        .alert-error {
            background-color: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }

        .alert-info {
            background-color: #d1ecf1;
            color: #0c5460;
            border: 1px solid #bee5eb;
        }

        .loading {
            display: none;
            margin-top: 20px;
        }

        .spinner {
            border: 3px solid #f3f3f3;
            border-top: 3px solid #667eea;
            border-radius: 50%;
            width: 40px;
            height: 40px;
            animation: spin 1s linear infinite;
            margin: 0 auto;
        }

        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }

        .info-box {
            background-color: #f8f9fa;
            border-left: 4px solid #667eea;
            padding: 15px;
            margin-bottom: 20px;
            text-align: left;
        }

        .info-box h3 {
            color: #667eea;
            margin-bottom: 10px;
        }

        .info-box ul {
            margin-left: 20px;
            color: #666;
        }

        .info-box li {
            margin-bottom: 5px;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="logo">🖥️</div>
        <h1>WebVNC 系统初始化</h1>
        <p class="subtitle">首次运行需要绑定管理员账户</p>

        <div class="info-box">
            <h3>📋 初始化说明</h3>
            <ul>
                <li>管理员使用系统用户认证</li>
                <li>请输入一个存在的系统用户名</li>
                <li>该用户将拥有系统管理权限</li>
                <li>初始化后可添加协作者和观察者</li>
            </ul>
        </div>

        <div class="alert alert-success" id="successAlert"></div>
        <div class="alert alert-error" id="errorAlert"></div>
        <div class="alert alert-info" id="infoAlert"></div>

        <form id="initForm">
            <div class="form-group">
                <label for="adminUsername">管理员用户名：</label>
                <input type="text" id="adminUsername" name="adminUsername" required 
                       placeholder="请输入系统用户名" autocomplete="username">
            </div>

            <div class="form-group">
                <label for="adminPassword">系统密码：</label>
                <input type="password" id="adminPassword" name="adminPassword" required 
                       placeholder="请输入系统密码" autocomplete="current-password">
            </div>

            <button type="submit" class="btn" id="initBtn">
                🚀 初始化系统
            </button>
        </form>

        <div class="loading" id="loading">
            <div class="spinner"></div>
            <p style="margin-top: 10px; color: #666;">正在初始化系统...</p>
        </div>
    </div>

    <script>
        document.getElementById('initForm').addEventListener('submit', async function(e) {
            e.preventDefault();
            
            const username = document.getElementById('adminUsername').value.trim();
            const password = document.getElementById('adminPassword').value;
            
            if (!username || !password) {
                showAlert('error', '请填写完整的用户名和密码');
                return;
            }
            
            // 显示加载状态
            document.getElementById('initBtn').disabled = true;
            document.getElementById('loading').style.display = 'block';
            hideAlerts();
            
            try {
                const response = await fetch('/api/initialize', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({
                        username: username,
                        password: password
                    })
                });
                
                const result = await response.json();
                
                if (result.success) {
                    showAlert('success', result.message);
                    
                    // 3秒后跳转到登录页面
                    setTimeout(() => {
                        window.location.href = '/';
                    }, 3000);
                } else {
                    showAlert('error', result.message || '初始化失败');
                }
            } catch (error) {
                console.error('初始化错误:', error);
                showAlert('error', '网络错误，请稍后重试');
            } finally {
                // 隐藏加载状态
                document.getElementById('initBtn').disabled = false;
                document.getElementById('loading').style.display = 'none';
            }
        });
        
        function showAlert(type, message) {
            hideAlerts();
            const alertElement = document.getElementById(type + 'Alert');
            alertElement.textContent = message;
            alertElement.style.display = 'block';
        }
        
        function hideAlerts() {
            document.querySelectorAll('.alert').forEach(alert => {
                alert.style.display = 'none';
            });
        }
        
        // 页面加载时检查初始化状态
        window.addEventListener('load', async function() {
            try {
                const response = await fetch('/api/check_init');
                const result = await response.json();
                
                if (result.initialized) {
                    showAlert('info', '系统已初始化，正在跳转到登录页面...');
                    setTimeout(() => {
                        window.location.href = '/';
                    }, 2000);
                }
            } catch (error) {
                console.error('检查初始化状态失败:', error);
            }
        });
    </script>
</body>
</html>
